package com.shuwen.gcdj.controller.basicsstrategy;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.DictResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.strategy.request.*;
import com.shuwen.gcdj.bean.strategy.response.StrategyPageResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.service.basicsstrategy.BasicsStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * @author :yys
 * 策略管理
 * @date : 2023/5/9 13:40
 */
@Api(tags = "基础运维 - 策略管理")
@RestController
@RequestMapping("/strategy")
@Slf4j
public class BasicsStrategyController extends AuthController {


    @Autowired
    private BasicsStrategyService strategyService;


    @ApiOperation(value = "获取策略列表分页信息")
    @PostMapping(value = "/getstrategypage")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<StrategyPageResp>> getStrategypage(@RequestBody StrategyPageReq req) {
        Page<StrategyPageResp> strategyPageResps = strategyService.getStrategypage(req);
        return CommonResult.success(CommonPage.restPage(strategyPageResps));
    }

    @ApiOperation(value = "获取策略模板列表")
    @GetMapping(value = "/getstategytemplatelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<StrategyPageResp>> getstategytemplatelist() {
        List<StrategyPageResp> strategyPageResps = strategyService.getstategytemplatelist();
        return CommonResult.success(strategyPageResps);
    }

    @ApiOperation(value = "添加策略")
    @PostMapping(value = "/addstrategy")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> add(HttpServletRequest request, @Valid @RequestBody AddStrategyReq req) {
        AddResp addResp = strategyService.add(initUser(request),req);
        return CommonResult.success(addResp);
    }

    @ApiOperation(value = "批量删除策略")
    @PostMapping(value = "/batchdeletestrategy")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> batchDelete(HttpServletRequest request, @RequestBody DeleteStrategyReq req) {
        VoidResp voidResp = strategyService.batchDelete(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "修改策略名称，状态，模板")
    @PostMapping(value = "/editstrategyname")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> editstrategyname(HttpServletRequest request, @Valid @RequestBody UpdateStrategyReq req) {
        VoidResp voidResp = strategyService.editstrategyname(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "策略关联设备信息")
    @PostMapping(value = "/editstrategylinkequipment")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> editstrategylinkequipment(HttpServletRequest request, @Valid @RequestBody AssociateDeviceReq req) throws ParseException {
        AddResp addResp = strategyService.editstrategylinkequipment(initUser(request),req);
        return CommonResult.success(addResp);
    }

    @ApiOperation(value = "获取关联设备")
    @PostMapping(value = "/getEquipment")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getEquipment(@RequestBody QueryEmentReq req){
        List<DictResp> list = strategyService.getEquipment(req);
        return CommonResult.success(list);
    }


}
